package org.firebug.spring.boot.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RabbitmqProducer {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void send(String exchange, String routingKey, String content) {
        logger.info("rabbitmq produce: {}", content);
        for (int i = 0; i < 1; i++) {
            Message message = MessageBuilder.withBody(("message: " + i).getBytes())
                    .setDeliveryMode(MessageDeliveryMode.PERSISTENT)
                    .setDeliveryTag(System.currentTimeMillis())
                    .build();
            rabbitTemplate.send(exchange, routingKey, message);
            logger.info("produce: " + i);
        }
    }
}
